home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / wedl120.zip / DEMO1.C < prev    next >
C/C++ Source or Header  |  1991-12-09  |  15KB  |  363 lines

  1.  
  2. /*---------------------------------------------------------------------------*/
  3. /*                                                                           */
  4. /*          WEDL - Windows Enhanced Dialog Library                           */
  5. /*          Copyright (c) 1991, Mike Smedley                                 */
  6. /*          All Rights Reserved                                              */
  7. /*          Module:  DEMO1.C                                                 */
  8. /*                                                                           */
  9. /*---------------------------------------------------------------------------*/
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "wedl.h"
  15. #include "demo.h"
  16. #include "demohelp.h"
  17.  
  18. /*---------------------------------------------------------------------------*/
  19.  
  20. static void update_date_text( HFORM hform );
  21.  
  22. /*---------------------------------------------------------------------------*/
  23.  
  24. struct states_t {
  25.     PSTR state_code;
  26.     int zip_low;
  27.     int zip_high;
  28. };
  29.  
  30. static struct states_t states[] = {
  31.     { "AK", 995, 999 },     /* Alaska               */
  32.     { "AL", 350, 369 },     /* Alabama              */
  33.     { "AR", 716, 729 },     /* Arkansas             */
  34.     { "AZ", 850, 865 },     /* Arizona              */
  35.     { "CA", 900, 961 },     /* California           */
  36.     { "CO", 800, 816 },     /* Colorado             */
  37.     { "CT",  60,  69 },     /* Connecticut          */
  38.     { "DE", 197, 199 },     /* Delaware             */
  39.     { "FL", 320, 349 },     /* Florida              */
  40.     { "GA", 300, 319 },     /* Georgia              */
  41.     { "HI", 967, 968 },     /* Hawaii               */
  42.     { "IA", 500, 528 },     /* Iowa                 */
  43.     { "ID", 832, 847 },     /* Idaho                */
  44.     { "IL", 600, 629 },     /* Illinois             */
  45.     { "IN", 460, 479 },     /* Indiana              */
  46.     { "KS", 641, 679 },     /* Kansas               */
  47.     { "KY", 400, 427 },     /* Kentucky             */
  48.     { "LA", 700, 714 },     /* Louisiana            */
  49.     { "MA",  10,  27 },     /* Massachusetts        */
  50.     { "MD", 206, 219 },     /* Maryland             */
  51.     { "ME",  39,  49 },     /* Maine                */
  52.     { "MI", 480, 499 },     /* Michigan             */
  53.     { "MN", 550, 567 },     /* Minnesota            */
  54.     { "MO", 630, 658 },     /* Missouri             */
  55.     { "MS", 386, 397 },     /* Mississippi          */
  56.     { "MT", 590, 599 },     /* Montana              */
  57.     { "NC", 270, 289 },     /* North Carolina       */
  58.     { "ND", 580, 588 },     /* North Dakota         */
  59.     { "NE", 680, 693 },     /* Nebraska             */
  60.     { "NH",  30,  38 },     /* New Hampshire        */
  61.     { "NJ",  70,  89 },     /* New Jersey           */
  62.     { "NM", 870, 884 },     /* New Mexico           */
  63.     { "NV", 889, 898 },     /* Nevada               */
  64.     { "NY", 100, 149 },     /* New York             */
  65.     { "OH", 430, 458 },     /* Ohio                 */
  66.     { "OK", 730, 749 },     /* Oklahoma             */
  67.     { "OR", 970, 979 },     /* Oregon               */
  68.     { "PA", 150, 196 },     /* Pennsylvania         */
  69.     { "RI",  27,  29 },     /* Rhode Island         */
  70.     { "SC", 290, 299 },     /* South Carolina       */
  71.     { "SD", 570, 577 },     /* South Dakota         */
  72.     { "TN", 370, 385 },     /* Tennessee            */
  73.     { "TX", 750, 885 },     /* Texas                */
  74.     { "UT", 840, 847 },     /* Utah                 */
  75.     { "VA", 220, 246 },     /* Virginia             */
  76.     { "VT",  50,  59 },     /* Vermont              */
  77.     { "WA", 980, 994 },     /* Washington           */
  78.     { "WI", 530, 549 },     /* Wisconsin            */
  79.     { "WV", 247, 268 },     /* West Virginia        */
  80.     { "WY", 820, 831 },     /* Wyoming              */
  81.     { "DC", 200, 205 },     /* District of Columbia */
  82.     { "GU",   0, 999 },     /* Guam                 */
  83.     { "PR",   0, 999 },     /* Puerto Rico          */
  84.     { "VI",   0, 999 },     /* Virgin Islands       */
  85.     { "ON",   0, 999 },     /* Ontario              */
  86.     { "PQ",   0, 999 },     /* Quebec               */
  87.     { "BC",   0, 999 },     /* British Columbia     */
  88.     { "AB",   0, 999 },     /* Alberta              */
  89.     { "SK",   0, 999 },     /* Saskatchewan         */
  90.     { "MB",   0, 999 },     /* Manitoba             */
  91.     { "NS",   0, 999 },     /* Nova Scotia          */
  92.     { "NB",   0, 999 },     /* New Brunswick        */
  93.     { "NF",   0, 999 },     /* Newfoundland         */
  94.     { "PE",   0, 999 },     /* Prince Edward Island */
  95.     { NULL,   0,   0 }      /* --- end of table --- */
  96. };
  97.  
  98. char zip_error_msg1[] = "Zip Code is incomplete";
  99. char zip_error_msg2[] = "Zip Code is invalid for given State";
  100. PSTR pzip_error_msg;
  101.  
  102. /*---------------------------------------------------------------------------*/
  103.  
  104. static PSTR months[] = {
  105.     NULL, "January", "February", "March", "April", "May", "June", "July",
  106.     "August", "September", "October", "November", "December"
  107. };
  108.  
  109. /*---------------------------------------------------------------------------*/
  110.  
  111. int FAR PASCAL DialogProc1( HWND hDlg, unsigned message, WORD wParam,
  112.                             LONG lParam )
  113. {
  114.     static HFORM hform = NULL;
  115.     static char first_name[16]="", mid_init[2]="", last_name[21]="";
  116.     static char address[31]="", city[16]="", state[3]="", zip_code[10]="";
  117.     static char soc_sec_no[10]="", phone_num[11]="", hire_date[9]="  /  /  ";
  118.  
  119.     switch( message ) {
  120.         case WM_INITDIALOG:
  121.             hform = form_begin( hDlg );
  122.             form_set_help( hform, "demohelp.hlp" );
  123.             field_define( hform, ID_SSN, soc_sec_no, DT_STRING,
  124.                           "<0..7>99'-'99'-'9(4)",
  125.                           FDF_NOTBLANK | FDF_UPDATE | FDF_COMPLETE,
  126.                           NULL, 0, HELPID_SSN );
  127.             field_define( hform, ID_FNAME, first_name, DT_STRING,
  128.                           "A(15)", FDF_CONDUPD | FDF_MIXED,
  129.                           NULL, 0, HELPID_FNAME );
  130.             field_define( hform, ID_MIDINIT, mid_init, DT_STRING,
  131.                           "A(1)'.'", FDF_UPDATE | FDF_UPPER,
  132.                           NULL, 0, HELPID_MIDINIT );
  133.             field_define( hform, ID_LNAME, last_name, DT_STRING,
  134.                           "<A..Z>A(19)", FDF_CONDUPD | FDF_MIXED,
  135.                           NULL, 0, HELPID_LNAME );
  136.             field_define( hform, ID_ADDRESS, address, DT_STRING,
  137.                           "?(30)", FDF_UPDATE | FDF_MIXED,
  138.                           NULL, 0, HELPID_ADDRESS );
  139.             field_define( hform, ID_CITY, city, DT_STRING,
  140.                           "?(15)", FDF_CONDUPD | FDF_MIXED,
  141.                           NULL, 0, HELPID_CITY );
  142.             field_define( hform, ID_STATE, state, DT_STRING,
  143.                           "A(2)", FDF_COMPLETE | FDF_UPDATE | FDF_UPPER,
  144.                           pcheck_state, BAD_STATE, HELPID_STATE );
  145.             field_define( hform, ID_ZIP, zip_code, DT_STRING,
  146.                           "<0..9>(5)'-'9(4)", FDF_UPDATE,
  147.                           pcheck_zip_code, BAD_ZIP, HELPID_ZIP );
  148.             field_define( hform, ID_PHONE, phone_num, DT_STRING,
  149.                           "'('999') '999'-'9999", FDF_UPDATE | FDF_COMPLETE,
  150.                           NULL, 0, HELPID_PHONE );
  151.             field_define( hform, ID_HIREDATE, hire_date, DT_STRING,
  152.                           " <01> 9 '/' <0123> 9 '/' <89> 9 ",
  153.                           FDF_UPDATE | FDF_COMPLETE | FDF_RDPHYS | FDF_WRPHYS,
  154.                           pcheck_date, BAD_DATE, HELPID_HIREDATE );
  155.             statmsg_define( hform, ID_INSERT, SM_INSERT, "Insert: On", "Insert: Off" );
  156.             form_end( hform );
  157.             return( TRUE );
  158.  
  159.         case WM_COMMAND:
  160.             dproc_enter_wm_command( hform, wParam, lParam );
  161.             switch( wParam ) {
  162.                 case BAD_DATE:
  163.                     dproc_enter_error( hform );
  164.                     MessageBox( hDlg, "Date Is Invalid", NULL, MB_OK );
  165.                     dproc_exit_error( hform, lParam );
  166.                     return( TRUE );
  167.                 case BAD_STATE:
  168.                     dproc_enter_error( hform );
  169.                     MessageBox( hDlg, "Invalid State Code", NULL, MB_OK );
  170.                     dproc_exit_error( hform, lParam );
  171.                     return( TRUE );
  172.                 case BAD_ZIP:
  173.                     dproc_enter_error( hform );
  174.                     MessageBox( hDlg, pzip_error_msg, NULL, MB_OK );
  175.                     dproc_exit_error( hform, lParam );
  176.                     return( TRUE );
  177.                 case ID_HIREDATE:
  178.                     if( HIWORD( lParam ) ==  EN_KILLFOCUS
  179.                      && !form_in_error_cond( hform )
  180.                      && !form_is_cancelled( hform ) ) {
  181.                         update_date_text( hform );
  182.                         return( TRUE );
  183.                     }
  184.                     break;
  185.                 case IDOK:
  186.                     dproc_enter_idok( hform );
  187.                     if( !form_in_error_cond( hform ) ) {
  188.                         if( form_validate( hform ) != NULL ) break;
  189.                         form_process( hform );
  190.                         EndDialog( hDlg, TRUE );
  191.                         form_terminate( hform );
  192.                         wsprintf( tbuf,
  193.                                   "Soc Sec No.\t= %s\n"
  194.                                   "Name\t\t= %s %s. %s\n"
  195.                                   "Address\t\t= %s\n"
  196.                                   "\t\t= %s, %s %.5s-%s\n"
  197.                                   "Phone No.\t= %s\n"
  198.                                   "Hire Date\t= %s",
  199.                                   (LPSTR) soc_sec_no,
  200.                                   (LPSTR) first_name,
  201.                                   (LPSTR) mid_init, (LPSTR) last_name,
  202.                                   (LPSTR) address, (LPSTR) city,
  203.                                   (LPSTR) state, (LPSTR) zip_code,
  204.                                   (LPSTR) zip_code+5, (LPSTR) phone_num,
  205.                                   (LPSTR) hire_date );
  206.                         MessageBox( NULL, tbuf, "Field Contents", MB_OK );
  207.                     }
  208.                     return( TRUE );
  209.                 case IDCANCEL:
  210.                     dproc_enter_idcancel( hform );
  211.                     EndDialog( hDlg, TRUE );
  212.                     form_terminate( hform );
  213.                     return( TRUE );
  214.             }
  215.             break;
  216.     }
  217.     return( FALSE );
  218. }
  219.  
  220. /*---------------------------------------------------------------------------*/
  221.  
  222. int FAR PASCAL check_date( LPSTR pbuf )
  223. {
  224.     char date[10];
  225.     int month, day, year;
  226.  
  227.     if( !str_is_blank( pbuf ) ) {
  228.  
  229.         /* parse year, day, and month from buffer */
  230.         lstrcpy( date, pbuf );
  231.         year = 1900 + atoi( date+4 );
  232.         *( date+4 ) = '\0';
  233.         day = atoi( date+2 );
  234.         *( date+2 ) = '\0';
  235.         month = atoi( date );
  236.  
  237.         /* validate month */
  238.         if( month > 12 ) return( 1 );
  239.  
  240.         /* validate day and month */
  241.         if( day < 1 ) return( 3 );
  242.         switch( month ) {
  243.             case 2:
  244.                 if( year%4 )
  245.                     if( day > 29 ) return( 3 );
  246.                 else
  247.                     if( day > 28 ) return( 3 );
  248.                 break;
  249.             case 1:
  250.             case 3:
  251.             case 5:
  252.             case 7:
  253.             case 8:
  254.             case 10:
  255.             case 12:
  256.                 if( day > 31 ) return( 3 );
  257.                 break;
  258.             case 4:
  259.             case 6:
  260.             case 9:
  261.             case 11:
  262.                 if( day > 30 ) return( 3 );
  263.                 break;
  264.             default:
  265.                 return( 1 );
  266.         }
  267.     }
  268.  
  269.     /* return zero indicating no error */
  270.     return( 0 );
  271. }
  272.  
  273. /*---------------------------------------------------------------------------*/
  274.  
  275. int FAR PASCAL check_state( LPSTR pbuf )
  276. {
  277.     int i;
  278.  
  279.     /* allow state to be blank */
  280.     if( str_is_blank( pbuf ) ) return( 0 );
  281.  
  282.     /* do for all state codes in the table */
  283.     for( i = 0 ; states[i].state_code != NULL ; i++ )
  284.  
  285.         /* if state code was found, then no error */
  286.         if( !lstrcmp( states[i].state_code, pbuf ) ) return( 0 );
  287.  
  288.     /* not a legal 2-letter state code */
  289.     return( 1 );
  290. }
  291.  
  292. /*---------------------------------------------------------------------------*/
  293.  
  294. int FAR PASCAL check_zip_code( LPSTR pbuf )
  295. {
  296.     HFORM hform;
  297.     HFIELD hfield;
  298.     LPSTR p;
  299.     int i, num_spaces;
  300.     long zip, zip_low, zip_high;
  301.  
  302.     /* allow zip code to be blank */
  303.     if( str_is_blank( pbuf ) ) return( 0 );
  304.  
  305.     /* count spaces in the extended portion of the 9-digit zip code */
  306.     num_spaces = 0;
  307.     for( p = pbuf + 5 ; *p != '\0' ; p++ ) if( *p == ' ' ) num_spaces++;
  308.  
  309.     /* if zip code isn't exactly 5 or 9 digits, then there's an error */
  310.     if( num_spaces != 0 && num_spaces != 4 ) {
  311.         pzip_error_msg = zip_error_msg1;
  312.         return( 6 );
  313.     }
  314.  
  315.     *( pbuf+5 ) = '\0';
  316.     hform = form_get_active();
  317.     hfield = field_get_from_ctrl_id( hform, ID_ZIP );
  318.     field_log_to_data( hfield, pbuf, &zip, DT_LONG );
  319.  
  320.     /* find matching state */
  321.     hfield = field_get_from_ctrl_id( hform, ID_STATE );
  322.     field_get_text( hfield, tbuf, FALSE );
  323.     for( i = 0 ; states[i].state_code != NULL ; i++ ) {
  324.         if( !lstrcmp( tbuf, states[i].state_code ) ) break;
  325.     }
  326.     if( states[i].state_code == NULL ) return( 0 );
  327.  
  328.     /* test zip code */
  329.     zip_low  = ( (long) ( states[i].zip_low ) ) * 100L;
  330.     zip_high = ( (long) ( states[i].zip_high ) ) * 100L;
  331.     if( zip >= zip_low && zip <= zip_high ) return( 0 );
  332.     pzip_error_msg = zip_error_msg2;
  333.     return( 1 );
  334. }
  335.  
  336. /*---------------------------------------------------------------------------*/
  337.  
  338. static void update_date_text( HFORM hform )
  339. {
  340.     LPFORM lpform;
  341.     int day, month, year;
  342.     char text[128], buf[9], date[10];
  343.  
  344.     field_get_text( field_get_from_ctrl_id( hform, ID_HIREDATE ), date, TRUE );
  345.  
  346.     lstrcpy( buf, date );
  347.     year = 1900 + atoi( buf+6 );
  348.     *( buf+5 ) = '\0';
  349.     day = atoi( buf+3 );
  350.     *( buf+2 ) = '\0';
  351.     month = atoi( buf );
  352.  
  353.     if( month >= 1 && month <= 12 )
  354.         wsprintf( text, "(%s %d, %d)", (LPSTR) months[ month ], day, year );
  355.     else
  356.         *text = '\0';
  357.  
  358.     lpform = form_lock( hform );
  359.     SetDlgItemText( lpform->hdlg, ID_HIRETEXT, text );
  360.     form_unlock( hform );
  361. }
  362.  
  363.